package com.example.springtest.people.controller;


import com.example.springtest.common.R;
import com.example.springtest.people.pojo.dto.PeopleDTO;
import com.example.springtest.people.pojo.entity.People;
import com.example.springtest.people.pojo.vo.AvatarVO;
import com.example.springtest.people.pojo.vo.PeopleVO;
import com.example.springtest.people.service.PeopleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * 学生控制层
 */

@RestController
@RequestMapping("people")
@Slf4j
@Api(tags="用户模块")
public class PeopleController {
     /**
      * 查询所有学生
      */
     @Autowired
     private PeopleService peopleService;

     @GetMapping("list")
     @ApiOperation("查询学生")
     public R<List<PeopleVO>> getPeopleList(){
          List<PeopleVO> peopleVOList = peopleService.getPeopleList();
          return R.ok(peopleVOList);
     }

     /**
      * 新增学生
      * @param
      * @return
      */
     @PostMapping("add")
     @ApiOperation("新增学生")
     public R<Void> addPeople(@RequestBody @Validated PeopleDTO peopleDTO) {
          log.debug("新增学生：{}", peopleDTO);
          peopleService.addPeople(peopleDTO);
          return R.ok();
     }



     /**
      * 根据学号查询学生信息
      * @param id
      * @return
      */
     @GetMapping("select/{id}")
     @ApiOperation("根据学号查询学生")
     public R<People> getPeople(@PathVariable Integer id){
          log.debug("学生ID：{}",id);
      People people=peopleService.getPeople(id);
      return R.ok(people);
     }

     /**
      * 删除学生
      * @param id
      * @return
      */
     @DeleteMapping("delete/{id}")
     @ApiOperation("根据学号删除学生")
     public R<Void> deletePeople(@PathVariable Integer id){
          peopleService.deletePeople(id);
          return R.ok();
     }

     /**
      * 编辑学生
      * @param id
      * @param peopleDTO
      * @return
      */
     @PutMapping("edit/{id}")
     @ApiOperation("编辑学生")
     public R<Void> editPeople(@PathVariable Integer id,@RequestBody @Validated PeopleDTO peopleDTO){
      log.debug("编辑学生：{}",peopleDTO);
      peopleService.editPeople(id,peopleDTO);
      return R.ok();
     }

     /**
      * 上传头像
      * @param id
      * @param avatar
      * @return
      */
     @PostMapping("uploadAvatar/{id}")
     @ApiOperation("上传学生头像")
     public R<Void> uploadAvatar(@PathVariable Integer id, @RequestParam("avatar") MultipartFile avatar) {
          log.debug("上传头像，学生ID：{}", id);
          peopleService.uploadAvatar(id, avatar);
          return R.ok();
     }

     /**
      * 获取头像集合
      * @return
      */
     @GetMapping("avatarList")
     public R<List<AvatarVO>> getAvatarList(){
             List<AvatarVO> avatarVOList= peopleService.getAvatarList();
             return R.ok(avatarVOList);

     }
}
